package lava.rt.wrapper;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import java.util.function.BiFunction;


public class FileWrapper extends BaseWrapper<File>{

	
	
	public FileWrapper(File self) throws IOException {
		super(self);
		if(!self.isFile()) {
			throw new IOException(self+" is not a file");
		}
	}

	
	
   
    
     private final static String CSV_SPLIT=",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
	
	public   List<String[]> readCsv() throws Exception {
		List<String[]> rets=new ArrayList<>();
		List<String> lines=readLines();
		for(String line: lines) {
			String[] row=line.trim().split(CSV_SPLIT, -1);
			rets.add(row);
		}
		
		return rets;
	}
    
	
	    
	    
	    public  String readContent() throws FileNotFoundException, IOException{
			 StringBuffer ret=new StringBuffer();
			   
			   try(    
					   FileReader fileReader=new FileReader(self);
					   BufferedReader reader = new BufferedReader(fileReader);){
	          
	         String line = null;  
	         
	         while((line=reader.readLine())!=null){  
	        
	             ret.append(line);
	             
	          }
			 }
			   
			 return ret.toString();
		}
	    
	    public  List<String> readLines() throws FileNotFoundException, IOException{
			 List<String> ret=new ArrayList<>();
			  
			   try(    
					   FileReader fileReader=new FileReader(self);
					   BufferedReader reader = new BufferedReader(fileReader);){
	          
	         String line = null;  
	         while((line=reader.readLine())!=null){  
	        
	             ret.add(line);  
	             
	          }
			   }
			   
			   return ret;
		}
		
		
		public void readLines(BiFunction<Integer,String,Integer> handler) throws FileNotFoundException, IOException{
			 
			   
			   try(    
					   FileReader fileReader=new FileReader(self);
					   BufferedReader reader = new BufferedReader(fileReader);){//换成你的文件名 
	         
	        String line = null;  
	        int lineIndex=0,lineStep=0;
	        while((line=reader.readLine())!=null&&lineStep>=0){  
	               if(lineStep>0) {
	            	   lineStep--;
	            	   continue;
	               }
	               lineStep=handler.apply(lineIndex,line);
	            
	         }
			}
			   
			   
		}
	    
	
}
